﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TfsBugger.Core;
using TfsBugger.Util;
using TfsBugger.Util.Ui;

namespace TfsBugger.Dialogs
{
    /// <summary>
    /// Interaction logic for ServerConnectDialog.xaml
    /// </summary>
    public partial class ServerConnectDialog : Window
    {
        #region Ctor
        public ServerConnectDialog()
        {
            InitializeComponent();
        } 
        #endregion


        #region Fields
        private string m_serverName = "tfs06.codeplex.com";
        #endregion


        #region Methods 
        /// <summary>
        /// Connects to the specified TFS server and does authentication.
        /// </summary>
        /// <returns></returns>
        private TfsServerManager Connect()
        {
            TfsServerManager svr = new TfsServerManager(m_serverName, this.chkUseHttps.IsChecked.Value, this.txtPortNum.Text);
            svr.UserAuthenticated += (sender, e) => Cursor = Cursors.Wait;
            try
            {
                svr.Load();
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "A connection with the server could not be established.  Details:\n\n"
                    + ex.Message,
                    "Connection Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);

                return null;
            }

            return svr;
        }
        #endregion


        #region Event Handlers 
        /// <summary>
        /// Sets the default server name on window load.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            this.txtServerName.Text = m_serverName;
            this.lstServers.ItemsSource = App.Servers;
        }


        /// <summary>
        /// Handles a cancel command.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCancelButtonClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }


        /// <summary>
        /// Handles an OK command.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnOkButtonClick(object sender, RoutedEventArgs e)
        {

            //var selProjs = from i in this.lstProjects.SelectedItems.Cast<object>()
            //               select ((ProjItemWrapper)i).Proj;

            //App.SelectedProjects.AddRange
            DialogResult = true;
            Close();
        }


        /// <summary>
        /// Handles a 'connect' command and loads both the 
        /// TFS server and associated projects into the UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnConnectButtonClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtServerName.Text))
            {
                MessageBox.Show(
                    "You must enter a TFS server name.",
                    "Missing Server Name",
                    MessageBoxButton.OK,
                    MessageBoxImage.Asterisk);
            }

            m_serverName = this.txtServerName.Text;
            using (UiWaitScope wait = new UiWaitScope(this))
            {
                TfsServerManager mgr = Connect();
                App.RegisterServer(mgr);
                this.lstServers.ItemsSource = App.Servers;
            }
        }


        private void OnServerSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.btnOk.IsEnabled = null != this.lstServers.SelectedItem;
        }
        #endregion
    }
}
